using System;
using System.Runtime.InteropServices;

namespace GameFrame
{
    [Serializable]
    [StructLayout(LayoutKind.Explicit)]
    public struct QBoolean : IEquatable<QBoolean>
    {
        public const int SIZE = 4;
        [FieldOffset(0)]
        public int Value;

        public static implicit operator bool(QBoolean b) => b.Value != 0;

        public static implicit operator QBoolean(bool b)
        {
            QBoolean qboolean;
            qboolean.Value = b ? 1 : 0;
            return qboolean;
        }

        public static unsafe void Serialize(void* ptr, FrameSerializer serializer)
        {
            QBoolean* qbooleanPtr = (QBoolean*) ptr;
            serializer.Stream.Serialize(ref qbooleanPtr->Value);
        }

        public static unsafe void Print(QBoolean* p0, FramePrinter printer) => printer.AddLine(string.Format("{0}", (object) (bool) *p0));

        public override string ToString() => string.Format("{0}", (object) (this.Value == 1));

        public bool Equals(QBoolean other) => this.Value == other.Value;

        public override bool Equals(object obj) => obj is QBoolean other && this.Equals(other);

        public override int GetHashCode() => this.Value;
    }
}